<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <el-form ref="formData" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userInfo.password" show-password style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
                <el-button @click="resetFields">重置</el-button>
                <el-button @click="$router.back()">取消</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <userInfoVue />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <jobInfoVue />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUsers } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import userInfoVue from './components/user-info.vue'
import jobInfoVue from './components/job-info.vue'
saveUserDetailById
export default {
  components: {
    userInfoVue,
    jobInfoVue
  },
  data() {
    return {
      id: this.$route.params.id,
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  async created() {
    //  获取员工基本信息
    const res = await getUsers(this.id)
    // 覆盖掉后端返回的密文密码
    this.userInfo = { ...res, password: '' }
  },
  methods: {
    saveUser() {
      // 这里有个缺陷，由于后端返回的密文长度超过规定的12位，为了真正修改密码，要设置一个值去存我们修改的值
      this.$refs.formData.validate(async vail => {
        if (vail) {
          await saveUserDetailById({ ...this.userInfo, id: this.id })
          this.$message.success('更新成功')
        } else {
          this.$message.warning('更新失败')
        }
      })
    },
    // 重置
    resetFields() {
      this.$refs.formData.resetFields() // 重置表单
    }
    // back() {
    //   window.history.back()
    // }
  }
}
</script>

<style scoped>

</style>
